package com.android.dwt.ui;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageButton;

import com.android.dwt.R;
import com.android.dwt.data.GTDApp;
import com.android.dwt.widget.CalendarView;


public class CalendarAct extends Activity implements OnClickListener {
	
	private CalendarView mCalView;
	private Button mToday;
	public Button mViewTask;
	public Button mAddTask;
	
	public GTDApp mGa;
	
	private SimpleDateFormat mSdfDate = new SimpleDateFormat("yyyy-MM-dd");

	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		mGa = (GTDApp) getApplication();
		
//		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.cal_agenda);
//		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.tb_calendar);
		
		mCalView = (CalendarView) findViewById(R.id.cv_cal);
		
		mViewTask = (Button) findViewById(R.id.btn_view_task);
		mViewTask.setOnClickListener(this);
		
		mAddTask = (Button) findViewById(R.id.btn_add_task);
		mAddTask.setOnClickListener(this);
		
		Calendar cal = Calendar.getInstance();
		String date = mSdfDate.format(cal.getTime());
		updateViewState(date);
		
		ImageButton ibReturn = (ImageButton) findViewById(R.id.ib_cal_return);
		ibReturn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				finish();
			}
			
		});
		ibReturn.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					
					v.setBackgroundResource(R.drawable.back_pressed);
					
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					
					v.setBackgroundResource(R.drawable.back);
				}

				return false;
			}
			
		});
		
		mToday = (Button) findViewById(R.id.btn_cal_today);
//		mToday.setEnabled(false);
		mToday.setVisibility(View.INVISIBLE);
		mToday.setOnClickListener(this);
		
	}
	
	public void updateViewState(String date) {
		
		Cursor csr = mGa.getDbta().getDayTask(date);
		if (csr.getCount() == 0) {
			
			mViewTask.setEnabled(false);
			mViewTask.setText("查看");
			
		} else {
			
			mViewTask.setEnabled(true);
			mViewTask.setText("查看" + " (" + csr.getCount() + ")");
		}
		
		csr = null;
	}
	
	public void setTodayEnabled(boolean enabled) {
		
//		mToday.setEnabled(enabled);
		
		if (enabled) {
			
			mToday.setVisibility(View.VISIBLE);
			
		} else {
			
			mToday.setVisibility(View.INVISIBLE);
		}
		
		return;
	}
	
	public void showDiaryList(int year, int month, int day) {
		
		GregorianCalendar gc = new GregorianCalendar(year,month,day);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		Intent i = new Intent(this, DayView.class);
		
		i.putExtra("cal", gc);
		i.putExtra("selected_date", sdf.format(gc.getTime()));
		
		startActivity(i);
	}

	@Override
	public void onClick(View v) {
		
		Intent i = null;
		
		Calendar cal = mCalView.getCurrentDay();
		
		switch (v.getId()) {
		
    		case R.id.btn_cal_today:
    			
    			mCalView.gotoToday();
    			
    			break;
    			
    		case R.id.btn_view_task:
    			
    			i = new Intent(this, DayView.class);
    			
    			i.putExtra("selected_date", mSdfDate.format(cal.getTime()));
    			
    			startActivity(i);
    			
    			break;
    			
    		case R.id.btn_add_task:
    			
    			i = new Intent(this, NewTaskAct.class);
				
				i.putExtra("list_index", GTDApp.AGENDA);
				i.putExtra("new_task", true);
				
				i.putExtra("selected_date", mSdfDate.format(cal.getTime()));
				
				startActivityForResult(i, 0);
    			
    			break;
    
    		default:
    			break;
		}
		
		return;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
//		super.onActivityResult(requestCode, resultCode, data);
		
		Calendar cal = mCalView.getCurrentDay();
		String date = mSdfDate.format(cal.getTime());
		updateViewState(date);
		
		mCalView.updateCalendar();
	}
	
}
